package com.xm.controller.web;

import com.xm.pojo.Device;
import com.xm.pojo.DevicePosition;
import com.xm.pojo.Sysuser;
import com.xm.service.DevicePositionService;
import com.xm.service.DeviceService;
import com.xm.util.JSONResult;
import com.xm.util.page.PageUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by wzl on 2018/4/2.
 */
@Controller
@RequestMapping("/web/device")
public class DeviceController {
    @Resource
    private DeviceService deviceService;
    @Resource
    private DevicePositionService devicePositionService;

    /**
     * 跳转到设备列表页面方法
     * @return
     */
    @RequestMapping(value="/toDeviceList")
    public ModelAndView toDeviceList(HttpServletRequest request){
        ModelAndView modelAndView=new ModelAndView("senseDevice/senseDeviceList");
        Sysuser loginUser=(Sysuser)request.getSession().getAttribute("sysuser");
        modelAndView.addObject("sysuser",loginUser);
        return modelAndView;
    }

    /**
     * 跳转到设备添加页面方法
     * @return
     */
    @RequestMapping(value="/toDeviceAdd")
    public ModelAndView toDeviceAdd(HttpServletRequest request){
        ModelAndView modelAndView=new ModelAndView("senseDevice/senseDeviceAdd");
        Sysuser loginUser=(Sysuser)request.getSession().getAttribute("sysuser");
        modelAndView.addObject("sysuser",loginUser);
        return modelAndView;
    }

    /**
     * 跳转到设备编辑页面方法
     * @return
     */
    @RequestMapping(value="/toDeviceEdit")
    public ModelAndView toDeviceEdit(HttpServletRequest request){
        ModelAndView modelAndView=new ModelAndView("senseDevice/senseDeviceEdit");
        Sysuser loginUser=(Sysuser)request.getSession().getAttribute("sysuser");
        modelAndView.addObject("sysuser",loginUser);
        return modelAndView;
    }

    /**
     * 新增设备方法
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "/insertDevice", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult insertDevice(Device device, DevicePosition devicePosition,HttpServletRequest request) throws Exception {
        JSONResult jsonResult = new JSONResult();
        Sysuser loginUser=(Sysuser)request.getSession().getAttribute("sysuser");
        if(loginUser==null){
            jsonResult.setSuccess(false);
            jsonResult.setMsg("用戶未登陸！");
            return jsonResult;
        }
        deviceService.insert(device);
        devicePosition.setDeviceID(device.getDeviceId());
        devicePositionService.insert(devicePosition);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 删除设备方法
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "/deleteDevice", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult deleteDevice(Device device) throws Exception {
        JSONResult jsonResult = new JSONResult();
        deviceService.delete(device.getDeviceId());
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 修改设备方法
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "/updateDevice", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult updateDevice(Device device,DevicePosition devicePosition) throws Exception {
        JSONResult jsonResult = new JSONResult();
        deviceService.update(device);
        DevicePosition devicePositionInfo=new DevicePosition();
        devicePositionInfo.setDeviceID(device.getDeviceId());
        devicePositionInfo=devicePositionService.selectOne(devicePositionInfo);
        devicePosition.setId(devicePositionInfo.getId());
        devicePosition.setDeviceID(device.getDeviceId());
        devicePositionService.update(devicePosition);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 查询全部用户设备方法
     *
     * @param device
     * @return
     */
    @RequestMapping(value = "/selectDeviceList", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectDeviceList(Device device,HttpServletRequest request) {
        JSONResult jsonResult = new JSONResult();
        Sysuser loginUser = (Sysuser) request.getSession().getAttribute("sysuser");
        device.setUserId(loginUser.getId());
        List<Device> list = deviceService.selectList(device);
        jsonResult.setData(list);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 查询设备详情方法
     *
     * @param device
     * @return
     */
    @RequestMapping(value = "/selectDeviceDetail", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectDeviceDetail(Device device) {
        JSONResult jsonResult = new JSONResult();
        Device deviceInfo = deviceService.selectOne(device);
        jsonResult.setData(deviceInfo);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 分页查询全部用户设备方法
     *
     * @param device
     * @return
     */
    @RequestMapping(value = "/selectDevicePage", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectDevicePage(Device device, HttpServletRequest request) {
        JSONResult jsonResult=new JSONResult();
        Sysuser loginUser = (Sysuser) request.getSession().getAttribute("sysuser");
        device.setUserId(loginUser.getId());
        List<Device> deviceList=deviceService.selectPageList(device);
        int total=deviceService.queryTotal(device);
        int length=device.getLength();
        int start=device.getStart();
        PageUtils pager = new PageUtils(deviceList, total, length,start);
        jsonResult.setData(pager);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 批量删除设备
     */
    @RequestMapping(value ="/deleteDevices", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult deleteDevices(@RequestParam(value = "ids[]") Integer[] ids) {
        JSONResult jsonResult=new JSONResult();
        for(int i=0;i<ids.length;i++){
            int deviceId=ids[i];
            deviceService.delete(deviceId);
        }
        jsonResult.setSuccess(true);
        return jsonResult;
    }


    /**
     * 查询全部设备方法
     *
     * @param device
     * @return
     */
    @RequestMapping(value = "/selectAllList", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectAllList(Device device) {
        JSONResult jsonResult = new JSONResult();
        List<Device> list = deviceService.selectList(device);
        jsonResult.setData(list);
        jsonResult.setSuccess(true);
        return jsonResult;
    }
}
